package com.yjhb.android.sbz.activity

import android.view.View
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.adapter.RunningTaskUserAdapter
import com.yjhb.android.sbz.entity.CheckResult
import com.yjhb.android.sbz.entity.ManagerTask
import com.yjhb.android.sbz.mvp.managercenter.RunningTaskUserListPresenter
import com.yjhb.android.sbz.mvp.managercenter.RunningTaskUserView
import com.yjhb.android.sbz.tool.Tool
import com.jojo.android.zxlib.base.BaseRecyclerAdapterV2
import com.jojo.android.zxlib.tool.ZxToast
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener
import kotlinx.android.synthetic.main.activity_running_user_list.*


/**
 *  创建者 zx
 *  创建时间 2021/3/23 11:40
 */
class RunningTaskUserListActivity : BaseActivity<RunningTaskUserListPresenter>(), OnRefreshLoadmoreListener, RunningTaskUserView {

    private var managerTask: ManagerTask? = null
    private val size = 25
    private var current = 1
    private val data = mutableListOf<CheckResult>()
    private var adapter: RunningTaskUserAdapter = RunningTaskUserAdapter(this)

    override fun initPresenter(): RunningTaskUserListPresenter? {
        return RunningTaskUserListPresenter(this)
    }

    override fun getLayoutId() = R.layout.activity_running_user_list

    override fun initUI() {
        managerTask = intent.getSerializableExtra("taskManager") as ManagerTask
        emptyRecyclerView.emptyView = layout_empty
        emptyRecyclerView.adapter = adapter
        adapter.data = this.data
    }

    override fun initData() {
        smartRefreshLayout.autoRefresh()
        load(false)
    }

    private fun load(isLoadMore: Boolean) {
        managerTask?.let {
            if (isLoadMore) {
                ++current
            } else {
                current = 1
            }
            presenter.loadUserList(it.task.id, size, current)
        }
    }

    override fun regEvent() {
        smartRefreshLayout.setOnRefreshLoadmoreListener(this)
        back.setOnClickListener { finish() }
        adapter.setOnItemClick(object : BaseRecyclerAdapterV2.ItemClickListener<CheckResult?> {
            override fun onViewClick(view: View?, msg: String?, index: Int, data: CheckResult?) {

            }

            override fun onItemClick(index: Int, data: CheckResult?) {
                data?.let {
                    Tool.startChatActivity(activity, it.taskOrder.userId.toString(),it.nickName,5,null)
                }
            }
        })
    }

    override fun onLoadmore(refreshlayout: RefreshLayout?) {
        load(true)
    }

    override fun onRefresh(refreshlayout: RefreshLayout?) {
        load(false)
    }

    override fun onList(data: List<CheckResult>, isRefresh: Boolean) {
        smartRefreshLayout.finishLoadmore()
        smartRefreshLayout.finishRefresh()
        if (isRefresh) {
            this.data.clear()
        }
        this.data.addAll(data)
        adapter.notifyDataSetChanged()
    }

    override fun onFailure(code: Int, msg: String?) {
        ZxToast.showSuccessBottomToast(msg)
        smartRefreshLayout.finishLoadmore()
        smartRefreshLayout.finishRefresh()
    }
}